<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Document sans titre</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
.champs {	font-size: x-small;
	font-family: Verdana, Arial, Helvetica, sans-serif;
}
.Style1 {
	font-size: x-large;
	font-weight: bold;
	font-family: Verdana, Arial, Helvetica, sans-serif;
}
-->
</style>
</head>

<body>
Recherche avancées de contacts
<form action="" method="post" name="solde" id="solde">
		<table border="0" cellspacing="2">
        		<tr>
        				<td width="46" align="right">nom</td>
        				<td width="144"><input name="nom" type="text" class="champs" id="nom" value="<? echo $_POST['nom']; ?>"></td>
        				<td width="331"><span class="champs">
        						<? mk_list_table_class("nom_auto",$_POST['nom_auto'],"SELECT nom FROM contacts WHERE nom!='' GROUP BY nom ORDER BY nom", "nom", "nom", $db, TRUE, "champs"); ?>
        				</span></td>
   				</tr>
        		<tr>
        				<td align="right">pr&eacute;nom</td>
        				<td><input name="prenom" type="text" class="champs" id="prenom" value="<? echo $_POST['prenom']; ?>"></td>
        				<td><span class="champs">
        						<? mk_list_table_class("prenom_auto",$_POST['prenom_auto'],"SELECT prenom FROM contacts WHERE prenom!='' GROUP BY prenom ORDER BY prenom  ", "prenom", "prenom", $db, TRUE, "champs"); ?>
        				</span></td>
   				</tr>
        		<tr>
        				<td align="right">soci&eacute;t&eacute;</td>
        				<td><input name="societe" type="text" class="champs" id="societe" value="<? echo $_POST['societe']; ?>"></td>
        				<td><span class="champs">
        						<? mk_list_table_class("societe_auto",$_POST['societe_auto'],"SELECT societe FROM contacts WHERE societe!='' GROUP BY societe ORDER BY societe", "societe", "societe", $db, TRUE, "champs"); ?>
        				</span></td>
   				</tr>
        		<tr>
        				<td align="right">adresse</td>
        				<td><input name="rue" type="text" class="champs" id="rue" value="<? echo $_POST['adresse']; ?>"></td>
        				<td><span class="champs">
        						<? mk_list_table_class("rue_auto",$_POST['rue_auto'],"SELECT rue FROM contacts WHERE rue!='' GROUP BY rue ORDER BY rue", "rue", "rue", $db, TRUE, "champs"); ?>
        				</span></td>
   				</tr>
        		<tr>
        				<td align="right">npa</td>
        				<td><input name="npa" type="text" class="champs" id="npa" value="<? echo $_POST['npa']; ?>" size="6" maxlength="10"></td>
        				<td><span class="champs">
        						<? mk_list_table_class("npa_auto",$_POST['npa_auto'],"SELECT npa FROM contacts WHERE npa!='' GROUP BY npa ORDER BY npa", "npa", "npa", $db, TRUE, "champs"); ?>
        				</span></td>
   				</tr>
        		<tr>
                  <td align="right">lieu</td>
                  <td><input name="lieu" type="text" class="champs" id="lieu" value="<? echo $_POST['lieu']; ?>"></td>
                  <td><span class="champs">
                    <? mk_list_table_class("lieu_auto",$_POST['lieu_auto'],"SELECT lieu FROM contacts WHERE lieu!='' GROUP BY lieu ORDER BY lieu", "lieu", "lieu", $db, TRUE, "champs"); ?>
                  </span></td>
      		  </tr>
        		<tr>
        				<td align="right">t&eacute;l</td>
        				<td><input name="tel" type="text" class="champs" id="tel" value="<? echo $_POST['tel']; ?>"></td>
        				 <td>je cherche: <? $tel=str_replace(array('+41','+33','.','-',' '),'',$_POST['tel']);
if (substr($tel,0,1)==0) $tel=substr($tel,1);
if (substr($tel,0,1)==0) $tel=substr($tel,1);
echo $tel ?></td> 
		  </tr>
        		</table>
		<p>		        <span class="champs">
                <input <? if ($_POST['solde']=="0") {print "checked";}?> type="radio" name="solde" value="0"><img src="../images/cpt_jaune_25.gif" alt="" width="25" height="25">
				<input <? if ($_POST['solde']=="-1") {print "checked";}?> type="radio" name="solde" value="-1">		<img src="../images/cpt_rouge_25.gif" width="25" height="25">
				<input <? if ($_POST['solde']=="1") {print "checked";}?> type="radio" name="solde" value="1">		<img src="../images/cpt_vert_25.gif" width="25" height="25">
				<input <? if ($_POST['solde']=="x" OR !$_POST['solde']) {print "checked";}?> type="radio" name="solde" value="x">        
				<span class="Style1">?<br>        </span>
        		<input name="rechercher" type="submit" value="Rechercher">
                <input name="rechercher" type="reset" value="Effacer tous les champs">
</p>
</form>
<table border=0 height=20>
<tr><td><img src="../images/separator2.gif" width="500" height="1"></td></tr></table>
<?
IF ($_POST['rechercher'])
{
//update_cpts($db);
if ($_POST['nom_auto']) {$_POST['nom']=$_POST['nom_auto'];}
if ($_POST['prenom_auto']) {$_POST['prenom']=$_POST['prenom_auto'];}
if ($_POST['societe_auto']) {$_POST['societe']=$_POST['societe_auto'];}
if ($_POST['rue_auto']) {$_POST['rue']=$_POST['rue_auto'];}
if ($_POST['npa_auto']) {$_POST['npa']=$_POST['npa_auto'];}
if ($_POST['lieu_auto']) {$_POST['lieu']=$_POST['lieu_auto'];}


$_POST['nom']=htmlspecialchars($_POST['nom'], ENT_NOQUOTES);
$_POST['prenom']=htmlspecialchars($_POST['prenom'], ENT_NOQUOTES);
$_POST['societe']=htmlspecialchars($_POST['societe'], ENT_NOQUOTES);
$_POST['rue']=htmlspecialchars($_POST['rue'], ENT_NOQUOTES);
$_POST['npa']=htmlspecialchars($_POST['npa'], ENT_NOQUOTES);
$_POST['lieu']=htmlspecialchars($_POST['lieu'], ENT_NOQUOTES);

$_POST['nom_auto']=htmlspecialchars($_POST['nom_auto'], ENT_NOQUOTES);
$_POST['prenom_auto']=htmlspecialchars($_POST['prenom_auto'], ENT_NOQUOTES);
$_POST['societe_auto']=htmlspecialchars($_POST['societe_auto'], ENT_NOQUOTES);
$_POST['rue_auto']=htmlspecialchars($_POST['rue_auto'], ENT_NOQUOTES);
$_POST['npa_auto']=htmlspecialchars($_POST['npa_auto'], ENT_NOQUOTES);
$_POST['lieu_auto']=htmlspecialchars($_POST['lieu_auto'], ENT_NOQUOTES);




$and=" WHERE ";
$sql="SELECT * FROM contacts ";
if ($_POST['nom'])
{
$sql=$sql.$and."nom LIKE '%".$_POST['nom']."%'";
$and=" AND ";
}
if ($_POST['prenom'])
{
$sql=$sql.$and."prenom LIKE '%".$_POST['prenom']."%'";
$and=" AND ";
}
if ($_POST['societe'])
{
$sql=$sql.$and."societe LIKE '%".$_POST['societe']."%'";
$and=" AND ";
}
if ($_POST['rue'])
{
$sql=$sql.$and."rue LIKE '%".$_POST['rue']."%'";
$and=" AND ";
}
if ($_POST['npa'])
{
$sql=$sql.$and."npa LIKE '%".$_POST['npa']."%'";
$and=" AND ";
}
if ($_POST['lieu'])
{
$sql=$sql.$and."lieu LIKE '%".$_POST['lieu']."%'";
$and=" AND ";
}
if ($_POST['tel'])
{
$tel=str_replace(array('+41','+33','.','-',' '),'',$_POST['tel']);
if (substr($tel,0,1)==0) $tel=substr($tel,1);
if (substr($tel,0,1)==0) $tel=substr($tel,1);

$tel_search='%';

for ($i=0; $i < strlen($tel); $i++)
	{
	$tel_search.=substr($tel,$i,1).'%';
	}
$sql="SELECT * FROM `contacts` WHERE tel_mob LIKE '".$tel_search."' OR tel_mob LIKE '".$tel_search."' OR tel_priv LIKE '".$tel_search."' OR tel_priv LIKE '".$tel_search."' OR tel_prof LIKE '".$tel_search."' OR tel_prof LIKE '".$tel_search."' "; 
} 

if ($_POST['solde']=="0")
{
$sql=$sql.$and."solde_en_notre_faveur='0'";
}
elseif ($_POST['solde']=="-1")
{
$sql=$sql.$and."solde_en_notre_faveur>'0'";
}
elseif ($_POST['solde']=="1")
{
$sql=$sql.$and."solde_en_notre_faveur<'0'";
}

//print "$sql";
$res=mysql_query($sql,$db) or die('Erreur SQL !<br>'.$sql.mysql_error());
$_SESSION['sql_client']=$sql;
print "<table border=0><tr><td><a href=\"?client_tri=1&tri=societe&search=".$_POST['client']."\">Société</a></td><td><a href=\"?client_tri=1&tri=nom&search=".$_POST['client']."\">Nom</a></td><td><a href=\"?client_tri=1&tri=prenom&search=".$_POST['client']."\">Prénom</a></td><td>action</td><td>solde en notre faveur</td></tr>";
while($ligne=mysql_fetch_assoc($res))
	{
	print '<tr><td>'.$ligne['societe'].'</td><td>'.$ligne['nom'].'</td><td>'.$ligne['prenom'].'</td><td><a href="?client_found='.$ligne['id'].'"><img src="../images/fleche_ve_dr25.gif" border=0></a>';
	
	/* $sql_cpt='SELECT * FROM cpt_comptes WHERE id_client='.$ligne['id'].'';
	$res_cpt=mysql_query($sql_cpt,$db);
	$ligne_cpt=mysql_fetch_assoc($res_cpt); */
	/* 
	$sql_jour='SELECT * FROM cpt_journal, cpt_comptes WHERE cpt_journal.id_client='.$ligne['id'].' AND cpt_comptes.id_client=cpt_journal.id_client AND no_compte_debit=cpt_comptes.no OR no_compte_credit=cpt_comptes.no ORDER BY date DESC';
	$res_jour=mysql_query($sql_jour,$db) or die('Erreur SQL !<br>'.$sql_jour.mysql_error()); 
	solde_en_notre_faveur
	$i=1;
	$nombre=10;
	$solde=0;
	$comment_comta="Extrait des $nombre dernières écritures";
	 while ($ligne_jour=mysql_fetch_assoc($res_jour))
		{
		//calcul du solde
			if ($ligne_jour['no_compte_debit']==$ligne_cpt['no'])
				{$solde=$solde+$ligne_jour['somme']; $texte="débit";}
			else
				{$solde=$solde-$ligne_jour['somme']; $texte="crédit";}
		
		//création d'un liste des x dernières écritures
		if ($i<=$nombre)
			{
			/* $comment_comta=$comment_comta."\n".format_datetime($ligne_jour->date,0).$texte.' de '.$ligne_jour['somme'].' Fr pour '.$ligne_jour['libelle']; 
			}
		$i++;
	
		} */
	$solde=$ligne['solde_en_notre_faveur'];
	$total_solde=$ligne['solde_en_notre_faveur']+$total_solde;

	//$comment_comta=$comment_comta."\nSolde en notre faveur: $solde";
	
	if ($solde<0){$img='cpt_vert_25.gif';}
	elseif($solde>0) {$img='cpt_rouge_25.gif';}
	else {$img='cpt_jaune_25.gif';}
	
	print mklink_simple('form.edit_contact.php?id='.$ligne['id'].'', 765, 700, 10, 10, impression_fiche_client);
	echo '<img src="../images/switch_info25.gif" alt="Editer" border="0"></a>
	<a href="print.client.php?id='.$ligne['id'].'" target="_blank"><img src="../images/printer25.gif" alt="version imprimable" border="0"></a>
	<a href="?compta_compte=105'.$ligne['id'].'"><img src="../images/'.$img.'" alt="'.$comment_comta.'" Title="'.$comment_comta.'" border="0"></a></td><td>'.$ligne['solde_en_notre_faveur'].'</td></tr>';

	}
echo '<tr><td></td><td></td><td></td><td><div align="right">tot :</div></td><td><b>'.$total_solde.'</b></td>';
print "</table>";
$_SESSION['client_search']=$_POST['client'];
	
}
?>
</body>
</html>
